package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AttributeAddNewDTO;
import cn.tedu.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.csmall.product.service.IAttributeService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *处理属性相关的的控制器
 *
 * @author stephen
 * @version 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/attributes")
@Api(tags = "07.属性管理模块")
public class AttributeController {

    @Autowired
    private IAttributeService attributeService;

    public AttributeController(){
        log.info("创建控制器：AttributeController");
    }

    /**
     * 添加属性
     * @param attributeAddNewDTO 封装提交过来的属性值
     * @return  返回结果
     */
    //http:localhost:9080/attributes/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加属性")
    @ApiOperationSupport(order = 100)
    public String addNew(@Validated AttributeAddNewDTO attributeAddNewDTO){
        log.debug("开始处理【添加属性】的请求,属性：{}",attributeAddNewDTO);
        attributeService.addNew(attributeAddNewDTO);
        return "添加属性成功";
    }

    /**
     * 根据id值删除属性
     * @param id 属性id
     * @return 返回响应结果
     */
    //http://localhost:9080/attributes/deleteById
    @PostMapping("/deleteById")
    @ApiOperation("根据id删除属性")
    @ApiOperationSupport(order = 200)
    public String deleteById(Long id){
        log.debug("开始处理【删除属性】的请求，属性id={}",id);
        attributeService.deleteById(id);
        return "删除成功";
    }

    /**
     * 查找属性列表
     * @param id 属性id
     * @return 属性列表的id
     */
    //http://localhost:9080/attributes/list
    @ApiOperation("获取属性列表")
    @ApiOperationSupport(order = 300)
    @GetMapping("/list")
    public JsonResult list(Long id){
        List<AttributeListItemVO> attributeListItemVOS = attributeService.listData(id);
        return JsonResult.ok(attributeListItemVOS);
    }
}
